package com.liucxu.qiushimeinv.View.Activity;

import android.support.v4.app.FragmentManager;
import android.support.v7.widget.Toolbar;
import android.widget.FrameLayout;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.liucxu.qiushimeinv.Base.BaseActivity;
import com.liucxu.qiushimeinv.Base.BaseFragment;
import com.liucxu.qiushimeinv.R;
import com.liucxu.qiushimeinv.Utils.ViewUtils;
import com.liucxu.qiushimeinv.View.Fragment.GirlsFragment;
import com.liucxu.qiushimeinv.View.Fragment.MeFragment;

public class MainActivity extends BaseActivity {

    private Toolbar toolbar;
    private BottomNavigationBar bottom_navigation_bar;
    private FrameLayout content_frame;
    private FragmentManager mFragmentManager;
    private BaseFragment girlsFragment;
    private BaseFragment meFragment;
    private BaseFragment currentFragment;

    @Override
    public void initBeforeView() {

    }

    @Override
    public int getInflateLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void setupView() {
        toolbar = $(R.id.toolbar);
        bottom_navigation_bar = $(R.id.bottom_navigation_bar);
        setSupportActionBar(toolbar);
        bottom_navigation_bar = $(R.id.bottom_navigation_bar);
        bottom_navigation_bar.setMode(BottomNavigationBar.MODE_SHIFTING);
        bottom_navigation_bar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        bottom_navigation_bar.setBarBackgroundColor(android.R.color.darker_gray);
        bottom_navigation_bar.setActiveColor("#0f88eb")
                .setInActiveColor("#d4d4d4")
                .setBarBackgroundColor("#f2f4f7");
        content_frame = $(R.id.content_frame);
        mFragmentManager = getSupportFragmentManager();
        bottom_navigation_bar.addItem(new BottomNavigationItem(R.mipmap.meinv, "美女"))
                .addItem(new BottomNavigationItem(R.mipmap.me, "我"))
                .initialise();
        bottom_navigation_bar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                switch (position) {
                    case 0:
                        if (!girlsFragment.isVisible()) {
                            mFragmentManager.beginTransaction().hide(currentFragment).show(girlsFragment).commitAllowingStateLoss();
                            currentFragment = girlsFragment;
                        }
                        break;
                    case 1:
                        if (meFragment == null) {
                            meFragment = ViewUtils.CreateFragment(MeFragment.class, true);
                            mFragmentManager.beginTransaction().hide(currentFragment).add(R.id.content_frame, meFragment).commitAllowingStateLoss();
                            currentFragment = meFragment;
                        } else {
                            if (!meFragment.isVisible()) {
                                mFragmentManager.beginTransaction().hide(currentFragment).show(meFragment).commitAllowingStateLoss();
                            }
                        }
                        currentFragment = meFragment;
                        break;
                }
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });
    }

    @Override
    public void setupData() {
        girlsFragment = ViewUtils.CreateFragment(GirlsFragment.class, true);
        mFragmentManager.beginTransaction().add(R.id.content_frame, girlsFragment).commitAllowingStateLoss();
        currentFragment = girlsFragment;
    }
}
